<?php
	defined( '_JEXEC' ) or die( 'Restricted access' );
	jimport( 'joomla.application.component.model' );
	require_once(JPATH_COMPONENT.DS.'libraries'.DS.'core.php' );
	include_once(JPATH_COMPONENT.DS.'libraries'.DS.'core.php');
	require_once(JPATH_COMPONENT.DS.'data'.DS.'implFusionTables'.DS.'FTDAOFactory.php');
	
	class ModelSocialRoutesRouteList extends JModel
	{
		var $_routes = null;
		var $_userID = null;
		var $_total = null;
		//var $_pageNav = null;
		
		function __construct()
		{
			parent::__construct();
			
//			$mainframe = JFactory::getApplication();
//
//			// Get the pagination request variables
//			$limit = $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', 4, 'int' );
//			$limitstart = JRequest::getVar('limitstart', 0);
//			
//			// Set the limit variable for query later on
//			$this->setState('limit', $limit); 
//			$this->setState('limitstart', $limitstart);
		}
		
		private function getUserMyRoutes(){
			//Take routes from MyRoutes
			$daoUserRoutes=JDAOFactory::getInstance()->getDAORoute();
			$myAddedRoutes=$daoUserRoutes->getUserRoutes($this->_userID);

			return $myAddedRoutes;
		}
		
		private function getUserCreatedRoutes(){
			//Take the routes which I am the creator of
			$daoJRoutes=JDAOFactory::getInstance()->getDAORoute();
			$myCreatedRoutes=$daoJRoutes->getUserCreatedRoutes($this->_userID);
	
            return $myCreatedRoutes;
		}
		
		private function deleteRepeatedRoutes($routes){
			for($i=0;$i<count($routes);$i++){
				if(isset($routes[$i])){
					for($j=$i+1;$j<count($routes);$j++){
						if(isset($routes[$j])&&($routes[$j]->getId()==$routes[$i]->getId())){
							unset($routes[$j]);
						}
					}
				}
			}
			return array_values($routes);
		}
		
		private function getLocation($keywords){
			$geocoder=new SRGeocoder();
	        $location=null;
	        $location=$geocoder->geocode($keywords);
	        return $location;
		}
		
		function searchRoutes(){
			//GET REQUEST PARAMETERS
			$keywords = JRequest::getVar('keywords', null);
			$circleRadius = JRequest::getVar('circleRadius', 10); 
			if($circleRadius==0){
				$circleRadius=10;
			}
			$circleRadius=$circleRadius*1000; 
			$maxDistance = JRequest::getVar('maxDistance', ''); 
			
			$daoFTRoutes=FTDAOFactory::getInstance()->getDAORoute();
			if($keywords!='' && $keywords!=null){
				//GET ROUTES BY KEYWORDS IN TITLE
				$routes=$daoFTRoutes->getRoutesDescByKeywords($keywords,$maxDistance);
	            //GET ROUTES BY PROXIMITY TO AN AREA
	            if($keywords!=""){
	            	$location=$this->getLocation($keywords);
					$nearestRoutes=$daoFTRoutes->getNearestRoutes($location,$circleRadius,$maxDistance);
	            	foreach($nearestRoutes as $route){
						$routes[] =$route;
	            	}
		      	}
	            $routes=$this->deleteRepeatedRoutes($routes);
			}
			else{
				$routes=$daoFTRoutes->getRoutesByDistance($maxDistance);
			}
            $this->_total=count($routes);
            $this->_routes=$routes;
            
            return $this->_routes;
		}
		
//		function getPagination(){
//			jimport('joomla.html.pagination');
//			if(empty($this->_pageNav)){
//				$this->_pageNav = new JPagination($this->_total, $this->getState('limitstart'), $this->getState('limit'));
//			}
//			return $this->_pageNav;	
//		}
		
		function getUserRoutes(){ 
			$this->_userID=JRequest::getVar('userid', CFactory::getUser()->get('id'));
			
			$myAddedRoutes=$this->getUserMyRoutes();
			$myCreatedRoutes=$this->getUserCreatedRoutes();
			
            $this->_routes=array_merge($myAddedRoutes,$myCreatedRoutes);
            $this->_total=count($this->_routes);
            return $this->_routes;
		}
		
		function getUser(){
			return CFactory::getUser($this->_userID);
		}
		
		function getRoutes(){
			return array_slice($this->_routes,$this->getState('limitstart'),$this->getState('limit'));
		}
	}
?>